package com.quark.framework.i18n;

import java.util.ArrayList;
import java.util.List;

import com.quark.framework.base.QuarkEnvironment;
import com.quark.framework.configuration.QuarkConfigManager;
import com.quark.framework.utility.QuarkLog;

public class QuarkI18nManager {
	
	public static final int english = 1;
	//for manager
	public static final int manager = 0;
	private ThreadLocal<Integer> tlsCurLanguage = null;
	List<QuarkI18nTextProvider> languageList = new ArrayList<QuarkI18nTextProvider>();
	static private QuarkI18nManager inst = new QuarkI18nManager();
	
	public QuarkI18nTextProvider getI18nTextProvider(int language)
	{
		if (0 <= language && language < languageList.size())
		{
			return languageList.get(language);
		}
		return null;
	}
	
	static public QuarkI18nManager getInstance()
	{
		return inst;
	}
	
	public QuarkI18nTextProvider getCurrentLanguageProvider()
	{
		return getI18nTextProvider(tlsCurLanguage.get());
	}
	
	public void setCurrentLanguage(Integer language)
	{
		if(language == null)
		{
			QuarkI18nConfiguration config = (QuarkI18nConfiguration)QuarkConfigManager.getInstance().getConfiguration(QuarkI18nConfiguration.class);
			language = config.curLanguage;
		}
		tlsCurLanguage.set(language);
	}
	
	public Integer getCurrentLanguage()
	{
		return tlsCurLanguage.get();
	}
	public boolean init()
	{
		QuarkI18nConfiguration config = (QuarkI18nConfiguration)QuarkConfigManager.getInstance().getConfiguration(QuarkI18nConfiguration.class);
		tlsCurLanguage = new ThreadLocal<Integer>();
		tlsCurLanguage.set(QuarkI18nManager.english);
		if(config.i18nFileList == null || config.i18nFileList.length == 0)
			return false;
		for(int i = 0; i < config.i18nFileList.length; i ++)
		{
			QuarkI18nTextProvider provider = new QuarkI18nTextProvider();
			int count = provider.loadFromFile(QuarkEnvironment.getInstance().getWorkingPath() + "config\\" + config.i18nFileList[i]);
			languageList.add(provider);
			QuarkLog.logDebug("I18n: " + count + " words loaded from " + config.i18nFileList[i]);
		}
		return true;
	}
}
